// information/promotionList/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  addPromotion: function () {
    wx.navigateTo({
      url: '../addPromotion/index',
    })
  },
  toEdit: function (e) {
    let pmid = e.currentTarget.dataset.pmid;
    wx.navigateTo({
      url: '../addPromotion/index?pmid=' + pmid,
    })
  },
  // 删除单个促销活动
  deletePromotion: function (e) {
    let that = this;
    let pmid = e.currentTarget.dataset.pmid;
    wx.showModal({
      title: '提示',
      content: '确认删除该促销活动？',
      success: function (ress) {
        if (ress.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Promotion/DeleteDiscount?pmId=' + pmid + '&uid=' + app.globalData.userInfo.Uid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '删除成功',
                  mask: true
                })
                that.getPromotionList();
              }
            }
          })
        }
      }
    })
  },
  getPromotionList: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Promotion/GetDiscountList?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          let list = [];
          if (res.data.Data.Items.length > 0) {
            list = res.data.Data.Items;
            that.setData({
              promotionList: list
            })
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(app.globalData.userInfo){
      this.getPromotionList();
    }else{
      setTimeout(function(){
        that.onShow();
      }.bind(this),1000)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})